package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONObject;
import io.xxx.xbutler.data.RobotMapper;
import io.xxx.xbutler.domain.Callback;
import io.xxx.xbutler.domain.Robot;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 机器人封号回调
 *
 * <pre>
 * {
 *     "nType": 1010,
 *     "vcMerchantNo": "",
 *     "vcRobotWxId": "",
 *     "vcRobotSerialNo": "",
 *     "vcSerialNo": "",
 *     "nResult": 1,
 *     "vcResult": "SUCCESS",
 *     "Data": {
 *         "vcRobotWxId": "",
 *         "vcRobotSerialNo": "",
 *         "vcNickName": "",
 *         "vcBase64NickName": "",
 *         "vcHeadImgUrl": "",
 *         "nSex": 0,
 *         "vcCountry": "",
 *         "vcProvince": "",
 *         "vcCity": "",
 *         "vcBase64WhatsUp": "",
 *         "nChatRoomCount": 0,
 *         "dtDateTime": "",
 *         "vcReason": "当前帐号的使用存在异常，为保护帐号安全，系统将其自动置为保护状态，被限制登录，如需继续使用，请轻触“确定”申请解除限制。",
 *         "vcBanType": ""
 *     }
 * }
 * </pre>
 */
@Slf4j
@Component
public class _1010Handler implements CallbackHandler {

    @Resource
    private RobotMapper robotMapper;

    @Override
    public String name() {
        return "机器人封号回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 1010;
    }

    @Override
    public void handle(Callback callback) {
        LocalDateTime now = LocalDateTime.now();
        JSONObject data = (JSONObject) callback.getData();

        Robot robot = (Robot) new Robot()
                .setBlockReason(data.getString("vcReason"))
                .setId(callback.getRobotId())
                .setUpdatedTime(now);
        robotMapper.updateById(robot);
    }
}
